(function() {
  app.controller('marketCtrl', function($state, $location, $scope, quoteApi, local_m, $rootScope, header_m, $interval) {
    var obj = [
      { code: '1A0001', market: 1, },
      { code: '399001', market: 2, },
      { code: '399006', market: 2, }
    ];

    $scope.board = [];
    $scope.init = function() {
      angular.forEach(obj, function(item, index) {
        item.sublist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 34, 35];
        var p = new quoteApi.QuoteApi.Price0x21(item);
        quoteApi.query(p).then(function(data) {
          $scope.board[index] = data;
        });
      });
    };

    $scope.go = function(type) {
      $location.path('/market/' + type).replace();
    }
    $scope.go_detail = function(item) {
      $scope.storage.local('detail_codeinfo', item.stkinfo);
      if (item.stkinfo.style === 2) {
        $state.go('detail');
      } else if (item.stkinfo.style === 1) {
        $state.go('detail_zs');
      }
    };

    $scope.go_top = function(type, sort, board_id) {
      $scope.storage.session('top_params', { type: type, sort: sort, board_id: board_id });
      $state.go('top');
    };

    $scope.bankuai_go = function(item) {
      $scope.storage.session('top_params', { type: 'bankuai2', sort: item.boardId, title: item.stkinfo.name });
      $state.go('top');
    };

    var timer = $interval(function() {
      $scope.init('reload');
    }, 3000);
    $scope.$on('$destroy', function() {
      $interval.cancel(timer);
      timer = null;
    });
  });
})();